/********************************************************************************
* File Name:	welcomeAdmin.cpp
* Description:	第10章实例训练          
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1  
* E_mail: openlinux2011@gmail.com		                
*
********************************************************************************/
#include "welcomeAdmin.h"
#include <QSqlRecord>

//管理员登录界面构造函数
WelcomeAdmin::WelcomeAdmin(QWidget* parent) : QDialog(parent)
{
    setupUi(this);
    adminAddCardModel = new QSqlTableModel(this);
    adminAddCardModel->setTable("card"); //绑定card表
    adminAddCardModel->setEditStrategy(QSqlTableModel::OnManualSubmit); //OnManualSubmit表明我们要提交修改才能使其生效
    adminAddCardModel->select(); //执行select()函数更新视图信息

    adminAddUserModel = new QSqlRelationalTableModel(this);
    adminAddUserModel->setTable("Student"); //绑定Student表
    adminAddUserModel->setEditStrategy(QSqlRelationalTableModel::OnManualSubmit); //OnManualSubmit表明我们要提交修改才能使其生效
    adminAddUserModel->setRelation(5,QSqlRelation("card","scardNumber","money")); //通过scardNumber将card表中的money与Student表绑定
    adminAddUserModel->select(); //执行select()函数更新视图信息
    tvAdmin->setModel(adminAddUserModel); //表与视图绑定

    /*信号与槽函数的连接*/
    connect(submitStuButton, SIGNAL(clicked()),this,SLOT(submitStuButton_clicked())); //添加新用户
    connect(pbDelete, SIGNAL(clicked()),this,SLOT(pbDelete_clicked())); //删除选中用户
    connect(pbSubmit, SIGNAL(clicked()),this,SLOT(pbSubmit_clicked())); //提交更改信息
    connect(pbSearch, SIGNAL(clicked()),this,SLOT(pbSearch_clicked())); //根据用户名查找
    connect(pbInputMoney, SIGNAL(clicked()),this,SLOT(pbInputMoney_clicked())); //充值处理
}

void WelcomeAdmin::submit() //提交事物处理
{
    adminAddUserModel->database().transaction(); //开始事务操作
    if (adminAddUserModel->submitAll())
    {
        adminAddUserModel->database().commit(); //提交
        QMessageBox::about(this, tr("操作stu表"),tr("提交成功!"));
    }
    else
    {
        adminAddUserModel->database().rollback(); //回滚
        QMessageBox::warning(this, tr("adminAddUserModel"),tr("数据库错误: %1").arg(adminAddUserModel->lastError().text()));
    }
}

void WelcomeAdmin::submitStuButton_clicked() //增加新用户
{
     int ok=0;
     QString sname,snumber,sclass,sdom,sphoneNumber,scardNumber,spasswd,stype,ssex;
     QString spasswd2;

     /*获取输入新用户的信息*/
     sname = leRealName->text();
     snumber = leUserName->text();
     sclass = leClassNumber->text();
     sdom = leDomNumber->text();
     sphoneNumber = lePhoneNumber->text();
     scardNumber = leCardNumber->text();
     spasswd = leUserPassword->text();
     stype = leUserType->text();
     ssex = leSex->text();
     spasswd2 = leUserPassword_2->text();

     /*判断姓名、学号、饭卡号是否为空*/
     ok = (sname.isEmpty())||(snumber.isEmpty())||(scardNumber.isEmpty());
     if(ok)
     {
         QMessageBox::critical(this,tr("错误!"),tr("姓名，学号或卡号为空!"));
     }
     else //不为空增加新用户
     {
         ok = spasswd.compare(spasswd2); //判断密码是否正确，相等为0
         if(ok) //两个密码不相等
         {
             QMessageBox::critical(this,tr("错误!"),tr("密码不匹配!"));
         }
         else
         {
              ok = QMessageBox::warning(this,tr("添加用户"),tr("你确定添加用户吗？"),QMessageBox::Yes,QMessageBox::No);
              if(ok == QMessageBox::No)
              {            
                  return;
              }
              else
              {    
                  QString insertStr="insert into card(scardNumber) values('"+scardNumber+"')";
                  QSqlQuery query;
                  if(query.exec(insertStr)) //如果card表提交成功，再进行Student表的更改
                  {
                      QMessageBox::about(this, tr("操作card表"),tr("提交成功!"));
                      insertStr="insert into Student(sname,snumber,sclass,sdom,sphonenumber,scardNumber,spasswd,stype,ssex) values('"
                              +sname+"','"+snumber+"','"+sclass+"','"+sdom+"','"+sphoneNumber+"','"+scardNumber+"','"+spasswd+"','"+stype+"','"+ssex+"')";
                      if(query.exec(insertStr))
                      {
                          QMessageBox::about(this, tr("操作stu表"),tr("提交成功!"));
                          adminAddUserModel->select();
                      }
                  }
              }
         }
     }
}

void WelcomeAdmin::pbDelete_clicked() //删除用户
{
    int curRow = tvAdmin->currentIndex().row(); //获取选中的行
    QSqlQuery query;
    QString scardNumber,snumber,queryStr;

    snumber=adminAddUserModel->record(curRow).value("snumber").toString();//获取对应的snumber值
    queryStr="select scardNumber from Student where snumber='"+snumber+"'";
    query.exec(queryStr); //获取与snumber对应的scardNumber的值
    if(query.next())
    {
        scardNumber=query.value(0).toString();
    }
    else
        return;

    adminAddUserModel->removeRow(curRow);  //删除该行
    int ok = QMessageBox::warning(this,tr("删除当前行!"),tr("你确定删除当前行吗？"),QMessageBox::Yes,QMessageBox::No);
    if(ok == QMessageBox::No)
    {
       adminAddUserModel->revertAll(); //如果不删除，则撤销
    }
    else
    {
        adminAddUserModel->submitAll(); //否则提交，在数据库中删除该行
        queryStr="delete from card where scardNumber='"+scardNumber+"'"; //删除card表中对应的卡信息
        if(query.exec(queryStr))
            QMessageBox::about(this, tr("删除用户"),tr("删除成功!"));
    }
}

void WelcomeAdmin::pbSubmit_clicked() //更改用户信息
{
    submit();
}

void WelcomeAdmin::pbSearch_clicked() //根据用户名模糊匹配搜索用户信息
{
    QString sname;
    sname = leSearchName->text();
    if(sname.isEmpty()) //如果输入信息为空，则选择所有用户信息
    {
        adminAddUserModel->setFilter(QObject::tr("snumber like '%1'").arg("%"));
        adminAddUserModel->select();
    }
    else
    {       
        adminAddUserModel->setFilter(QObject::tr("snumber like '%1'").arg((QString)("%")+sname+(QString)("%"))); //根据姓名进行筛选
        adminAddUserModel->select(); //显示结果 
    }
}

void WelcomeAdmin::pbInputMoney_clicked() //饭卡充值
{
     int ok=0;
     QString scardNumber,inMoney;
     int nowMoney,sumMoney;
     QSqlQuery queryMoney;
     QString strMoney;
     scardNumber = leCardNumber_2->text(); //获取卡号
     inMoney = leInMoney->text();    
     ok = (scardNumber.isEmpty())||(inMoney.isEmpty());
     if(ok)
     {
         QMessageBox::critical(this,tr("错误!"),tr("学号或预存金额为空!"));
     }
     else
     {        
         queryMoney.exec("select money from card where scardNumber='"+scardNumber+"'");  //选择当前卡中剩余金额
         if(queryMoney.next())
         {
             QString now = queryMoney.value(0).toString();
             leMoney->setText(now); //显示当前剩余金额
             nowMoney=queryMoney.value(0).toInt();
             sumMoney=inMoney.toInt()+nowMoney; //充值之后金额
             strMoney=QString::number(sumMoney);
             queryMoney.exec("update card set money = '"+strMoney+"' where scardNumber='"+scardNumber+"'");
             lbAllMoney->setText(strMoney); //显示最新金额
             QMessageBox::about(this, tr("操作card表"),tr("充值成功!"));
             adminAddUserModel->select(); //执行select()函数更新视图信息
         }
         else
             QMessageBox::warning(this, tr("操作card表"),tr("无此卡号！"));
     }
}



